/***************************************************
> Copyright (C) 2024 ==RXX== All rights reserved.
> File Name: 5.c
> Author: rxx
> Mail:2909017383@qq.com 
> Created Time: 2024年11月12日 星期二 16时45分54秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define N 10

int main(int argc,const char* argv[])
{
	srand(time(NULL));
	int array[10] = { 0 };
	int tmp = 0;
	int num = 0;
	int low = 0;
	int high = N-1;
	int i = 0;
	for(int i = 0 ; i < N ; i++){
		array[i] = rand() % 100;
	}
	printf("排序前：");
	for(int i = 0 ; i < N ; i++){
		printf("%d\t",array[i]);
	}
	printf("\n");

	for(int i = 0 ; i < N ; i++){
		for(int j = 0 ; j < N-1-i ; j++){
			if(array[j]>array[j+1]){
				tmp=array[j+1];
				array[j+1]=array[j];
				array[j]=tmp;
			}
		}
	}
	printf("排序后：");
	for(int i = 0 ; i < N ; i++){
		printf("%d\t",array[i]);
	}
	printf("\n");
	
	printf("输入一个数：");
	scanf("%d",&num);

	int mid = ( high + low ) / 2;
	
	while(low <= high){//当low小于high的时候
		if(  num > array[mid] ){
			low = mid + 1;//在mid的右边
			mid=(low+high)/2;
		}else if(num == array[mid]){
			tmp = mid;//等于跳出循环
			break;
		}else if(num < array[mid]){
			high = mid - 1;//在mid的左边
			mid = (low+high)/2;
		}
	}
	if(low > high){
		tmp=-1;
	}
	printf("%d\n",tmp);

    return 0;
}
